package com.sunb2b.pojo;

import java.util.*;

import com.sunb2b.dao.CompanyMemberDAO;
import com.sunb2b.dao.MemberDAO;
import com.sunb2b.dao.PersonMemberDAO;
import com.sunb2b.dao.UserDAO;
import com.sunb2b.entity.CompanyMember;
import com.sunb2b.entity.Member;
import com.sunb2b.entity.PersonMember;
import com.sunb2b.entity.User;

public class PageSearchResult {
	private int pageSize;
	private int page;
	private int total;
	private List items;
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getItems() {
		return items;
	}
	public void setItems(List items) {
		this.items = items;
	}
	
	public List<Map> getMemberList(){
		List<Map> result = new ArrayList<Map>();
		if(this.getItems()!=null && this.getItems().size()>0){
    		Iterator<Object> it = this.getItems().iterator();
    		while(it.hasNext()){
    			Object memberData = it.next();
    			Map<String, String> record = new HashMap<String, String>();
    			if(memberData instanceof CompanyMember){
    				System.out.println("It's a CompanyMember, added to list.");
    				
    				setMemberData(record, (Member)memberData);
    				setCompanyMemberSpecificData(record, (CompanyMember)memberData);
    				
    				
    			}else if (memberData instanceof PersonMember){
    				System.out.println("It's a PersonMember, added to list.");
    				setMemberData(record, (Member)memberData);
    				setPersonMemberSpecificData(record, (PersonMember)memberData);
    				
    			}else{
    				System.out.println("Not a Member, ignore "+memberData.getClass().getSimpleName());
    			}
    			result.add(record);
    		}
    	}
		return result;
	}
	
	private void setMemberData(Map<String, String> map, Member member){
		map.put("id", member.getId().toString());
		map.put(MemberDAO.NAME, member.getName());
		map.put(MemberDAO.USERNAME, member.getUsername());
		map.put(MemberDAO.DESCRIPTION, member.getDescription());
		map.put(MemberDAO.TEL, member.getTel());
		map.put(MemberDAO.FAX, member.getFax());
		map.put(MemberDAO.REGION_ID, member.getRegionId());
		map.put(MemberDAO.ADDRESS, member.getAddress());
		map.put(MemberDAO.MOBILE, member.getMobile());
		map.put(MemberDAO.STATUS, member.getStatus());
	}
	
	private void setCompanyMemberSpecificData(Map<String, String> map, CompanyMember company){
		map.put(CompanyMemberDAO.BIZ, company.getBiz());
		map.put(CompanyMemberDAO.LEGAL_PERSION, company.getLegalPerson());
		map.put(CompanyMemberDAO.TYPE, company.getType());
		map.put(CompanyMemberDAO.WEBSITE, company.getWebsite());
	}
	
	private void setPersonMemberSpecificData(Map<String, String> map, PersonMember person){
		map.put(PersonMemberDAO.GENDER, person.getGender());
	}
	
	public List<Map> getSystemAdminList(){
		List<Map> result = new ArrayList<Map>();
		if(this.getItems()!=null && this.getItems().size()>0){
    		Iterator<Object> it = this.getItems().iterator();
    		while(it.hasNext()){
    			Object userData = it.next();
    			Map<String, String> record = new HashMap<String, String>();
    			
    			record.put("id", ((User)userData).getId().toString());
    			record.put(UserDAO.USERNAME, ((User)userData).getUsername());
    			record.put(UserDAO.STATUS, ((User)userData).getStatus());
    			record.put(UserDAO.CREATEUSER, ((User)userData).getCreateUser());
    			record.put(UserDAO.CREATEDATE, (((User)userData).getCreateDate()==null)?null:((User)userData).getCreateDate().toLocaleString());
    			//System.out.println(userData.getClass().getSimpleName());
    			result.add(record);
    		}
    		
		}
		return result;
	}
}
